home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / bench / dustbuster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.0 KB  |  116 lines

  1. #include "tmp/postgres.h"
  2. #include "tmp/c.h"
  3.  
  4. #include "storage/bufpage.h"
  5. #include "utils/log.h"
  6.  
  7. #include "access/genam.h"
  8. #include "access/heapam.h"
  9. #include "access/tqual.h"
  10.  
  11. #include "catalog/pg_relation.h"
  12.  
  13. /*
  14.  *  dustbuster() -- a portable postgres vacuum cleaner.
  15.  *
  16.  *    This routine doesn't do any updates on its own behalf, so it
  17.  *    wouldn't really matter if it violated two-phase locking.  However,
  18.  *    due to a bug I don't understand in the lock manager, we can't
  19.  *    even do short-term write locks in the inner scan here -- the lock
  20.  *    manager bitches about an attempt to release a write lock that we
  21.  *    definitely have.
  22.  *
  23.  *    Since the only thing we update is stuff that other routines have
  24.  *    to fill in anyway, this shouldn't matter very much.  Still, you
  25.  *    might feel better running this single-user.
  26.  *
  27.  *    Takes no parameters; returns the number of tuples cleaned, in lieu
  28.  *    of any more interesting result.
  29.  */
  30.  
  31. int32
  32. dustbuster()
  33. {
  34.     Relation pgrel;
  35.     TupleDescriptor tupdesc;
  36.     HeapScanDesc relscan;
  37.     HeapScanDesc scan;
  38.     Name rname;
  39.     char rtype;
  40.     Relation onerel;
  41.     HeapTuple htup;
  42.     HeapTuple reltup;
  43.     Buffer buf, buf2;
  44.     Page page;
  45.     OffsetIndex offind, maxoff;
  46.     ItemId itemid;
  47.     int blkno, nblocks;
  48.     int nchanges;
  49.     Datum d;
  50.     Boolean tupchng, changed;
  51.     Boolean n;
  52.  
  53.     pgrel = heap_openr(Name_pg_relation);
  54.     tupdesc = RelationGetTupleDescriptor(pgrel);
  55.     relscan = heap_beginscan(pgrel, 0, NULL, 0, NULL);
  56.  
  57.     nchanges = 0;
  58.     while (HeapTupleIsValid(reltup = heap_getnext(relscan, 0, &buf))) {
  59.     if (!HeapTupleSatisfiesTimeQual(reltup, NowTimeQual))
  60.         continue;
  61.  
  62.     d = (Datum) heap_getattr(reltup, buf, Anum_pg_relation_relkind,
  63.                  tupdesc, &n);
  64.     rtype = (char) DatumGetChar(d);
  65.     if (rtype != 'r')
  66.         continue;
  67.  
  68.     d = (Datum) heap_getattr(reltup, buf, Anum_pg_relation_relname,
  69.                  tupdesc, &n);
  70.     rname = (Name) DatumGetName(d);
  71.     onerel = heap_openr(rname);
  72.     nblocks = RelationGetNumberOfBlocks(onerel);
  73.  
  74.     for (blkno = 0; blkno < nblocks; blkno++) {
  75.  
  76.         changed = false;
  77.         buf2 = ReadBuffer(onerel, blkno);
  78.         page = BufferGetPage(buf2, 0);
  79.         maxoff = PageGetMaxOffsetIndex(page);
  80.  
  81.         for (offind = 0; offind <= maxoff; offind++) {
  82.  
  83.         itemid = PageGetItemId(page, offind);
  84.         if (!ItemIdIsUsed(itemid))
  85.             continue;
  86.  
  87.         htup = (HeapTuple) PageGetItem(page, itemid);
  88.         tupchng = false;
  89.  
  90.         if (htup->t_tmin == 0 && TransactionIdIsValid(htup->t_xmin)
  91.             && TransactionIdDidCommit(htup->t_xmin)) {
  92.             htup->t_tmin = TransactionIdGetCommitTime(htup->t_xmin);
  93.             tupchng = changed = true;
  94.         }
  95.         if (htup->t_tmax == 0 && TransactionIdIsValid(htup->t_xmax)
  96.             && TransactionIdDidCommit(htup->t_xmax)) {
  97.             htup->t_tmax = TransactionIdGetCommitTime(htup->t_xmax);
  98.             tupchng = changed = true;
  99.         }
  100.         if (tupchng)
  101.             nchanges++;
  102.         }
  103.         if (changed) {
  104.         WriteBuffer(buf2);
  105.         } else
  106.         ReleaseBuffer(buf2);
  107.     }
  108.     heap_close(onerel);
  109.     }
  110.  
  111.     heap_endscan(relscan);
  112.     heap_close(pgrel);
  113.  
  114.     return (nchanges);
  115. }
  116.